﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SMSPanasonic.Business
{
    public partial class OperatorPrefixCode
    {
        #region Singleton pattern
        private static object _lock = new object();
        private static DataTable _PrefixCode;
        public static DataTable PrefixCode
        {
            get
            {
                if (_PrefixCode == null)
                {
                    _PrefixCode = new DataTable();
                    using (SqlDataReader rd = SqlHelper.ExecuteReader(AppConfig.ConnectionString, CommandType.Text,
                            "SELECT prefix_code, operator_code, operator_id, enabled, max_length, min_length FROM dbo.OperatorPrefixCode"))
                    {
                        _PrefixCode.Load(rd);
                    }
                }
                return _PrefixCode;
            }
        }
        private static Dictionary<string, int> _dictPrefixCode;
        public static Dictionary<string, int> DictionaryPrefixCode
        {
            get
            {
                if (_dictPrefixCode == null)
                {
                    _dictPrefixCode = new Dictionary<string, int>();
                    foreach (DataRow row in PrefixCode.Rows)
                    {
                        _dictPrefixCode.Add((string)row["prefix_code"], (int)row["operator_id"]);
                    }
                }
                return _dictPrefixCode;
            }
        }
        public static void ReloadData()
        {
            using (SqlConnection conn = new SqlConnection(AppConfig.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT prefix_code, operator_code, operator_id, enabled, max_length, min_length FROM dbo.OperatorPrefixCode", conn))
                {
                    ReloadData(cmd);
                }
            }
        }

        public static void ReloadData(SqlCommand cmd)
        {
            lock (_lock)
            {
                if (_PrefixCode != null)
                    _PrefixCode.Rows.Clear();
                else
                    _PrefixCode = new DataTable();
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    _PrefixCode.Load(rd);
                }
            }
        }
        #endregion
        public static int GetOperatorID(string phonenumber)
        {
            return GetOperatorID(phonenumber, DictionaryPrefixCode);
        }

        public static bool IsMobilePhone(string phonenumber)
        {
            try
            {
                if (GetOperatorID(phonenumber, DictionaryPrefixCode) != 9)
                    return true;
                return false;
            }
            catch
            {
                return false;
            }
        }

        public static int GetOperatorID(string phonenumber, Dictionary<string, int> source)
        {
            try
            {
                for (int i = 4; i <= 7; i++)
                {
                    string prefix = phonenumber.Substring(0,i);
                    if (source.ContainsKey(prefix))
                        return source[prefix];
                }
                return 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool ValidatePhoneNumber(string phonenumber)
        {
            var phone = phonenumber.ToNormalPhoneNumber();
            return PrefixCode.Rows.Cast<DataRow>()
                        .Any(row => phone.IndexOf(row["prefix_code"].ToString()) == 0
                            && phone.Length >= Convert.ToInt32(row["min_length"])
                            && phone.Length <= Convert.ToInt32(row["max_length"])
                    );
        }

        public static bool ValidatePhoneNumber(string[] phonenumber)
        {
            foreach (string item in phonenumber)
            {
                if (!ValidatePhoneNumber(item))
                    return false;
            }
            return true;
        }

        public static bool ValidatePhoneNumber(IEnumerable<string> phonenumber)
        {
            foreach (string item in phonenumber)
            {
                if (!ValidatePhoneNumber(item))
                    return false;
            }
            return true;
        }
    }
}
